

package com.hazelcast.scheduledexecutor.impl.operations;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.scheduledexecutor.ScheduledTaskHandler;
import com.hazelcast.scheduledexecutor.impl.ScheduledExecutorDataSerializerHook;
import com.hazelcast.spi.impl.operationservice.MutatingOperation;
import com.hazelcast.spi.impl.operationservice.Operation;

import java.io.IOException;

public class DisposeTaskOperation extends AbstractBackupAwareSchedulerOperation implements MutatingOperation {

    private String taskName;

    public DisposeTaskOperation() {
    }

    public DisposeTaskOperation(ScheduledTaskHandler descriptor) {
        super(descriptor.getSchedulerName());
        this.taskName = descriptor.getTaskName();
        setPartitionId(descriptor.getPartitionId());
    }

    @Override
    public void run() throws Exception {
        getContainer().dispose(taskName);
    }

    @Override
    public Operation getBackupOperation() {
        return new DisposeBackupTaskOperation(getSchedulerName(), taskName);
    }

    @Override
    public int getClassId() {
        return ScheduledExecutorDataSerializerHook.DISPOSE_TASK_OP;
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        super.writeInternal(out);
        out.writeString(taskName);
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        super.readInternal(in);
        this.taskName = in.readString();
    }
}
